package com.sunricher.telinkblemeshlib.db;

import android.content.Context;

import com.sunricher.telinkblemeshlib.models.UartDaliDevice;

import java.util.List;

public class UartDaliDeviceManager {

    private UartDaliDeviceDatabase database;

    private UartDaliDeviceManager() {

    }

    public static UartDaliDeviceManager getInstance(Context context) {
        SingleHolder.instance.database = UartDaliDeviceDatabase.getInstance(context);
        return SingleHolder.instance;
    }

    private static class SingleHolder {
        private static final UartDaliDeviceManager instance = new UartDaliDeviceManager();
    }

    public UartDaliDevice getDevice(int daliAddress, int gatewayAddress) {
        return database.getDevice(daliAddress, gatewayAddress);
    }

    public List<UartDaliDevice> getDevices(int gatewayAddress) {
        return database.getDevices(gatewayAddress);
    }

    public void insertOrUpdate(UartDaliDevice device) {
        database.insert(device);
    }

    public void deleteAll(int gatewayAddress) {
        database.deleteAll(gatewayAddress);
    }

    public void delete(int daliAddress, int gatewayAddress) {
        database.delete(daliAddress, gatewayAddress);
    }

    public void testUartDaliDevice() {
        UartDaliDevice d1 = new UartDaliDevice(0, 100, UartDaliDevice.DEVICE_TYPE_DT6);
        insertOrUpdate(d1);
        List<UartDaliDevice> d1Select = getDevices(100);
        System.out.println("d1Select " + d1Select.size());
        d1.setName("Test D1");
        d1.setDeviceType(UartDaliDevice.DEVICE_TYPE_DT8_CCT);
        insertOrUpdate(d1);
        List<UartDaliDevice> d1Select2 = getDevices(100);
        System.out.println("d1Select2 " + d1Select.size());

        UartDaliDevice d2 = new UartDaliDevice(1, 100, UartDaliDevice.DEVICE_TYPE_DT8_XY);
        d2.setName("D2 XY");
        insertOrUpdate(d2);
        UartDaliDevice d3 = new UartDaliDevice(2, 200, UartDaliDevice.DEVICE_TYPE_DT8_RGBW);
        insertOrUpdate(d3);

        List<UartDaliDevice> devices = getDevices(100);
        System.out.println("devices " + devices.size());

        delete(0, 100);
        devices = getDevices(100);
        System.out.println("devices2 " + devices.size());

        deleteAll(100);
        System.out.println("devices " + devices.size());
    }
}
